﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Resources;

namespace Tuzzit
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructeur
        public MainPage()
        {
            InitializeComponent();
            sloganTuzz.Text = AppResources.SloganTuzzit;
            buttonLogin.Content = AppResources.BoutonLogin;
            //CreateAccount.Content = AppResources.NewAccount;
            ForgotPassword.Content = AppResources.ForgotPass;
            
        }

        private void PlaceholderLogin(object sender, RoutedEventArgs e)
        {
            Login.Text = string.Empty;
        }

        private void DefaultLoginPlaceholder(object sender, RoutedEventArgs e)
        {
            string.IsNullOrWhiteSpace(Login.Text);
        }

        private void ForgotPassword_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/RecoveryPassword.xaml", UriKind.RelativeOrAbsolute));
        }

        /*private void CreateAccount_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/NewAccount.xaml", UriKind.RelativeOrAbsolute));
        }*/

        //Quand on clique sur "Enter" après avoir entré son password
        private void ClickEnter(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                //Appel à la fonction de connexion
                Connect(sender, e);
            }
        }

        private void Connect(object sender, RoutedEventArgs e)
        {
            //Variables Locales
            String login;
            String password;

            login = Login.Text;
            password = Password.Password;
            //MessageBox.Show("Login : " + login + " - " + "Password : " + password);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://dev.tuzzit.com/exec/login.php?login=" + login + "&password=" + password);
            request.CookieContainer = new CookieContainer();
            request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
        }

        private void ReadWebRequestCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);

            (Application.Current as App).Cookies = myResponse.Cookies;
           
            (Application.Current as App).Cookies = myResponse.Cookies;

            using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
            {
                string results = httpwebStreamReader.ReadToEnd();
                //MessageBox.Show(results.ToString() //-- on another thread!
                Dispatcher.BeginInvoke(() => login_success(results.Trim()));
            }
            myResponse.Close();
        }

        private void login_success(string res)
        {
            //MessageBox.Show("#"+res.ToString()+"#");
            //String resResponse = res.Substring(res.IndexOf('5')).Replace("5", String.Empty);
            //resResponse = "@" + resResponse;
            //resResponse = Regex.Replace(resResponse, @"\n", "");
            //resResponse.Trim();
            //MessageBox.Show(resResponse);
            if (res.Equals("OK"))
            {
                NavigationService.Navigate(new Uri("/ProfilPivot.xaml", UriKind.RelativeOrAbsolute));
            }
           else
            {
                ResourceManager resourceManager = new ResourceManager("Tuzzit.AppResources", GetType().Assembly);
                MessageBox.Show((String)resourceManager.GetObject("MessageBoxBadLogin"));
            }
        }

        private void BecomeTuzzer(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/NewAccount.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}